package marchsoft.modules.tiku.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import marchsoft.modules.tiku.entity.EPracticeRecord;
import marchsoft.modules.tiku.mapper.EPracticeRecordMapper;
import marchsoft.modules.tiku.service.IEPracticeRecordService;
import marchsoft.base.BasicServiceImpl;
import marchsoft.utils.CacheKey;
import marchsoft.utils.RedisUtils;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;

/**
* <p>
* 题库练习记录 服务实现类
* </p>
*
* @author zsy
* @since 2022-03-29
*/
@Service
@RequiredArgsConstructor
public class EPracticeRecordServiceImpl extends BasicServiceImpl<EPracticeRecordMapper, EPracticeRecord> implements IEPracticeRecordService {

    private final EPracticeRecordMapper ePracticeRecordMapper;
    private final RedisUtils redisUtils;

    @Override
    public List<EPracticeRecord> getEPracticeRecordByIdForReids(Long id) {
        List<EPracticeRecord> ePracticeRecordList = new ArrayList<>();
//        EPracticeRecord ePracticeRecord = new EPracticeRecord();
        Map<String, Object> ePracticeRecordRedis = new HashMap<>();
        String key = CacheKey.PRACTICERECORD + id;

        if (redisUtils.hasKey(key)) {
            ePracticeRecordRedis =(Map<String, Object>) (Object) redisUtils.hmget(key);
            Set<String> keySet = ePracticeRecordRedis.keySet();
            for (Object keyReposiId : keySet) {
                EPracticeRecord ePracticeRecord = new EPracticeRecord();
                ePracticeRecord.setId(id);
                ePracticeRecord.setFinishRepoIds((String) keyReposiId);
                ePracticeRecord.setFinishQuesAnswer((String) ePracticeRecordRedis.get(keyReposiId));
                ePracticeRecordList.add(ePracticeRecord);
            }
            return ePracticeRecordList;
        }

        //若redis中的key已过期，说明练习记录已入库，此时，需要查数据库
        EPracticeRecord ePracticeRecord = ePracticeRecordMapper.selectById(id);
        if (ObjectUtil.isNotNull(ePracticeRecord)) {
            String repoidsString = ePracticeRecord.getFinishRepoIds();
            String answersString = ePracticeRecord.getFinishQuesAnswer();
            if (StrUtil.isNotBlank(repoidsString) && StrUtil.isNotBlank(answersString)) {
                String[] repoids = repoidsString.split(",");
                String[] answers = answersString.split(",");
                for (int i = 0; i < repoids.length; i++) {
                    EPracticeRecord ePracticeRecord1 = new EPracticeRecord();
                    ePracticeRecordRedis.put(repoids[i],answers[i]);
                    ePracticeRecord1.setFinishRepoIds(repoids[i]);
                    ePracticeRecord1.setFinishQuesAnswer(answers[i]);
                    ePracticeRecordList.add(ePracticeRecord1);
                }
                //放入缓存
                redisUtils.hmset(key, ePracticeRecordRedis);
            }
        }
        return ePracticeRecordList;
    }

    @Override
    public void addPracticeRecord(EPracticeRecord ePracticeRecord) {
        Long practiceRecordId = ePracticeRecord.getId();
        String key = CacheKey.PRACTICERECORD + practiceRecordId;
        long time = Duration.between(LocalDateTime.now(), LocalDateTime.now().plusDays(2L)).getSeconds();
        //用户练习记录存入缓存(key为练习记录id,map中的key为题目id，value为答案)
        String answer = ePracticeRecord.getFinishQuesAnswer();
        String repoId = ePracticeRecord.getFinishRepoIds();

        if (!redisUtils.hasKey(key)) {
            //没有此key，说明已经入库或者第一次添加记录
            // 初始化redis
            this.getEPracticeRecordByIdForReids(practiceRecordId);
        }

        redisUtils.hset(key, repoId, answer, time);
    }

}

